package yc.bluetooth.libvoice.util;

import android.os.Bundle;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;


/**
 * 作者：yeqianyun on 2020/1/17 11:01
 * 邮箱：1612706976@qq.com
 *
 * 服务器端向客户端发送消息
 * 客户端向服务器端发送消息
 */
public class ServerClientUtil {

    private static final String TAG = ServerClientUtil.class.getSimpleName();

    ////////////////////////////////////  Service中向客户端发送消息  //////////////////////////////
    /**
     * 服务器端向客户端发送消息 —— 无参
     *
     * @param fromClientMessenger 客户端中传递过来的Messenger
     * @param what            标记
     */
    public static void sendToClientMsg(Messenger fromClientMessenger, int what) {
        Message repoMsg = Message.obtain(null, what);
        try {
            // 向客户端发送
            fromClientMessenger.send(repoMsg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 服务器端向客户端发送消息 —— 一参
     *
     * @param fromClientMessenger 客户端中传递过来的Messenger
     * @param what            标记
     * @param ob              参数
     */
    public static void sendToClientMsg(Messenger fromClientMessenger, int what, Object ob) {
        Message repoMsg = Message.obtain(null, what, ob);
        try {
            // 向客户端发送
            fromClientMessenger.send(repoMsg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 服务器端向客户端发送消息 —— 两参
     *
     * @param fromClientMessenger 客户端中传递过来的Messenger
     * @param what            标记
     * @param ob              参数1
     * @param arg1            整型参数
     */
    public static void sendToClientMsg(Messenger fromClientMessenger, int what, Object ob,int arg1) {
        Message repoMsg = Message.obtain(null, what, ob);
        repoMsg.arg1 = arg1;
        try {
            // 向客户端发送
            fromClientMessenger.send(repoMsg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    ////////////////////////////////////  客户端向Service发送消息  //////////////////////////////

    /**
     * 客户端向服务器端发送消息 —— 无参
     * @param serverMessenger 服务器端Messenger
     * @param clientMessenger 当前客户端中Messenger
     * @param what 标记
     */
    public static void sendToServerMsg(Messenger serverMessenger,Messenger clientMessenger,int what) {
        if (serverMessenger == null) {
            Log.e(TAG,"sendToServerMsg(): serverMessenger == null");
            return;
        }
        Message msg = Message.obtain(null, what);
        msg.replyTo = clientMessenger; // 将客户端的Messenger传递给服务端
        try {
            serverMessenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 客户端向服务器端发送消息 —— 一参
     * @param serverMessenger 服务器端Messenger
     * @param clientMessenger 当前客户端中Messenger
     * @param what 标记
     * @param object 参数
     */
    public static void sendToServerMsg(Messenger serverMessenger,Messenger clientMessenger,int what,Object object) {
        if (serverMessenger == null) {
            Log.e(TAG,"sendToServerMsg(): serverMessenger == null");
            return;
        }
        Message msg = Message.obtain(null, what,object);
        msg.replyTo = clientMessenger; // 将客户端的Messenger传递给服务端
        try {
            serverMessenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 客户端向服务器端发送消息 —— 两参
     * @param serverMessenger 服务器端Messenger
     * @param clientMessenger 当前客户端中Messenger
     * @param what 标记
     * @param object 参数1
     * @param arg1 参数2（int）
     */
    public static void sendToServerMsg(Messenger serverMessenger,Messenger clientMessenger,int what,Object object,int arg1) {
        if (serverMessenger == null) {
            Log.e(TAG,"sendToServerMsg(): serverMessenger == null");
            return;
        }
        Message msg = Message.obtain(null, what,object);
        msg.arg1 = arg1;
        msg.replyTo = clientMessenger; // 将客户端的Messenger传递给服务端
        try {
            serverMessenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 客户端向服务器端发送消息 —— 两参
     * @param serverMessenger 服务器端Messenger
     * @param clientMessenger 当前客户端中Messenger
     * @param what 标记
     * @param b1 参数1
     * @param b2 参数2
     */
    public static void sendToServerMsg(Messenger serverMessenger,Messenger clientMessenger,int what,boolean b1,boolean b2) {
        if (serverMessenger == null) {
            Log.e(TAG,"sendToServerMsg(): serverMessenger == null");
            return;
        }
        Message msg = Message.obtain(null, what);
        Bundle bundle = new Bundle();
        bundle.putBoolean("isUpdateOta",b1);
        bundle.putBoolean("isUpdateNormal",b2);
        msg.setData(bundle);
        msg.replyTo = clientMessenger; // 将客户端的Messenger传递给服务端
        try {
            serverMessenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    /**
     * 客户端向服务器端发送消息 —— 两参
     * @param serverMessenger 服务器端Messenger
     * @param clientMessenger 当前客户端中Messenger
     * @param what 标记
     * @param b 参数1
     * @param s 参数2
     */
    public static void sendToServerMsg(Messenger serverMessenger,Messenger clientMessenger,int what,boolean b,String s) {
        if (serverMessenger == null) {
            Log.e(TAG,"sendToServerMsg(): serverMessenger == null");
            return;
        }
        Message msg = Message.obtain(null, what);
        Bundle bundle = new Bundle();
        bundle.putBoolean("isDevelopMode",b);
        bundle.putString("dataLogDir",s);
        msg.setData(bundle);
        msg.replyTo = clientMessenger; // 将客户端的Messenger传递给服务端
        try {
            serverMessenger.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}